﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyERP.Models;

namespace MyERP.WebApi
{
    /// <summary>
    /// 收款查询
    /// </summary>
    [Route("api/[controller]/[Action]")]
    [ApiController]
    [Authorize(Roles = "admin,q4")]
    public class ProceedsController : ControllerBase
    {
        private readonly MyDbContext db;
        public ProceedsController(MyDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 获取销售单总金额、已支付金额、未支付金额
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<Count> GetCount()
        {
            //获取销售单总金额
            double totalMoney = db.SalesTicket.Sum(x => x.Money);
            //获取已收款金额
            double money = db.Proceeds.Sum(x => x.PaymentMoney);
            //计算未收款金额
            double residueMoney = Math.Round(totalMoney - money, 2);

            Count c = new Count()
            {
                TotalMoney = totalMoney,
                Money = money,
                ResidueMoney = residueMoney
            };
            return c;
        }
        /// <summary>
        /// 获取未收款的客户的数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<notProceeds>> GetUserNotProceeds()
        {
            //获取客户总单据金额
            var ClientTotalSum = db.SalesTicket
                .GroupBy(u => u.ClientName)
                .Select(g => new
                {
                    ClientName = g.Key,
                    TotalAmount = g.Sum(o => o.Money)
                }).ToList();

            //获取已支付总金额
            var haveProceeds = db.Proceeds
                .GroupBy(u => u.Name)
                .Select(g => new
                {
                    Name = g.Key,
                    TotalAmount = g.Sum(o => o.PaymentMoney)
                }).ToList();

            //计算未支付金额
            List<notProceeds> up = new List<notProceeds>();
            foreach (var i in ClientTotalSum)
            {
                notProceeds u = new notProceeds()
                {
                    Name = i.ClientName,
                    Money = i.TotalAmount
                };
                foreach (var j in haveProceeds)
                {
                    if (i.ClientName == j.Name)
                    {
                        u.Money = Math.Round(i.TotalAmount - j.TotalAmount, 2);
                    }
                }
                if (u.Money == 0)
                {
                    continue;
                }
                up.Add(u);
            }
            return up;
        }
        /// <summary>
        /// 添加收款单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> Add(Proceeds p)
        {
            db.Proceeds.Add(p);
            await db.SaveChangesAsync();
            return Ok();
        }
        /// <summary>
        /// 查询最近十条收款单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Proceeds>> QueryTen()
        {
            List<Proceeds> ps = new List<Proceeds>();
            ps = await db.Proceeds.OrderByDescending(x => x.Id).Take(10).ToListAsync();
            return ps;
        }
    }
    public class notProceeds
    {
        public string Name { get; set; }
        public double Money { get; set; }
    }
}
